//
//  KLVipViewController.m
//  Meboo
//
//  Created by kyo on 2024/1/2.
//

#import "NSVipViewController.h"
#import "NSVipHeaderView.h"
#import "MBVipPopView.h"
#import "MBWebViewController.h"
#import "NSVipFuncsView.h"
#import "KLPaymentManager.h"
#import "NSVipBuyPopView.h"
@interface NSVipViewController ()
@property(nonatomic,strong) NSVipHeaderView *vipHeaderView;
@property(nonatomic,strong) NSVipFuncsView *vipFuncsView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *imageNameArr;
@property (nonatomic, strong) NSMutableArray *titleArr;
@property (nonatomic , strong) UICollectionView *collectionView;
@property (nonatomic , strong) MBVipPopView *vipPopView;

@property (nonatomic, strong) NSVipBuyPopView *vipBuyPopView;

@property (nonatomic , strong) UIButton *buyBtn;
@property(nonatomic,strong) UIImageView *bottomImageView;
//@property (nonatomic, strong) UIImageView *dateImageView;
////恢复购买
//@property (nonatomic, strong) UIButton *resumeBtn;
//
//支付协议
@property (nonatomic, strong) UILabel *privacyLab0;
//会员服务协议
@property (nonatomic, strong) UILabel *privacyLab1;
////续订协议
//@property (nonatomic, strong) UILabel *privacyLab2;
@property (nonatomic, copy) NSString *productID;
@end

@implementation NSVipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HEXS(@"ffffff");
    [self setupUI];
//    [self setVipState];
    [MBNotificationCenter addObserver:self selector:@selector(paySuccess) name:PAY_SUCCESS_NOTIFICATION object:nil];
}

#pragma mark - Private Method

- (void)paySuccess {
//    self.vipPopView.hidden = NO;
//    [self setVipState];
}
//- (void)setVipState {
//
//    if ([MBUserAccount defaultAccount].isVip) {
//        self.vipHeaderView.dateLab.text = [NSString stringWithFormat:@"Vip expiration time:%@",[MBUserAccount defaultAccount].vipDateStr];
//        self.vipHeaderView.vipDateView.hidden = NO;
//        [self.buyBtn setTitle:@"Unsubscribe" forState:0];
//        [self.buyBtn setBackgroundImage:ImageWithName(@"vip_purchase_back_1") forState:0];
//        [self.buyBtn setTitleColor:UIColor.whiteColor forState:0];
//        //debugTest
//        self.buyBtn.enabled = NO;
//        self.buyBtn.hidden = YES;
//        self.dateImageView.hidden = YES;
//    } else {
//        self.vipHeaderView.vipDateView.hidden = YES;
//        [self.buyBtn setBackgroundImage:ImageWithName(@"vip_purchase_back") forState:0];
//        [self.buyBtn setTitle:@"$29.9  Purchase" forState:0];
//        [self.buyBtn setTitleColor:HEXS(@"C07013") forState:0];
//        self.buyBtn.enabled = YES;
//        self.buyBtn.hidden = NO;
//        self.dateImageView.hidden = NO;
//    }
//}

- (void)PurchaseAction {
    __WeakSelf
    self.buyBtn.userInteractionEnabled = NO;
    [[KLPaymentManager sharePaymentManager] payWithProductID:self.productID];
    [KLPaymentManager sharePaymentManager].payBlock = ^(BOOL payState) {
        self.buyBtn.userInteractionEnabled = YES;
        if (payState) {
            [[MBLoginDataManager sharedManager] getUserInfoWithCode:@"" success:^(BOOL success) {
                [weakSelf.vipHeaderView resetVipDate];
            }];
            [kKeyWindow addSubview:weakSelf.vipBuyPopView];
            weakSelf.vipBuyPopView.hidden = NO;
            
            [weakSelf.vipFuncsView getProductData];
            /**
             埋点
             Me非会员点击进入会员订阅页支付成功Me_Nonmember_Premiumsuccess
             Me会员点击进入会员订阅页换购成功Me_Member_Premiumsuccess
             */
            [[NSIOApiManager sharedManager] setIOWithEventName:[MBUserAccount defaultAccount].isVip ? @"Me_Member_Premiumsuccess" : @"Me_Nonmember_Premiumsuccess"];
        }
    };
}

#pragma mark - Action

- (void)agreementClick0 {
    MBWebViewController *webVC = [[MBWebViewController alloc] init];
    [webVC loadWithURL:[NSURL URLWithString:paymentAgreementWeb]];
    webVC.titleStr = @"PaidPolicy";
    //    webVC.titleStr = @"PrivacyPolicy";
    [self.currentViewController.navigationController pushViewController:webVC animated:YES];
}

- (void)agreementClick1 {
    MBWebViewController *webVC = [[MBWebViewController alloc] init];
    [webVC loadWithURL:[NSURL URLWithString:MembershipServiceWeb]];
    webVC.titleStr = @"VIP Service Agreement";
    [self.currentViewController.navigationController pushViewController:webVC animated:YES];
}

#pragma mark - UI
- (void)setupUI {
    
    CGFloat height = ScreenWidth*244/375;
    [self.view addSubview:self.vipHeaderView];
    [self.vipHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(height);
    }];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.vipHeaderView.mas_bottom).offset(MBHeightScale(-24));
        make.right.left.bottom.equalTo(self.view);
        make.top.mas_equalTo(StatusBarAndNavigationBarHeight);
    }];
    
    [self.scrollView addSubview:self.vipFuncsView];
    [self.vipFuncsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.scrollView);
        make.width.mas_equalTo(ScreenWidth);
//        make.height.mas_equalTo(MBHeightScale(400));
        make.bottom.mas_equalTo(TabbarSafeBottomMargin);
    }];
    
//    [self.view addSubview:self.collectionView];
//    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.vipHeaderView.mas_bottom).offset(MBHeightScale(15));
//        make.right.left.equalTo(self.view);
//        make.bottom.mas_equalTo(MBWidthScale(-85)-TabbarSafeBottomMargin);
//    }];
    
    [self.view addSubview:self.bottomImageView];
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(MBWidthScale(92)+TabbarSafeBottomMargin);
    }];
    
        [self.view addSubview:self.buyBtn];
        [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(MBWidthScale(20));
            make.width.mas_equalTo(ScreenWidth-MBWidthScale(40));
            make.bottom.mas_equalTo(MBWidthScale(-20)-TabbarSafeBottomMargin);
            make.height.mas_equalTo(MBWidthScale(58));
        }];
//    
//    [self.view addSubview:self.dateImageView];
//    [self.dateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.buyBtn);
//        make.width.mas_equalTo(MBWidthScale(60));
//        make.height.mas_equalTo(MBHeightScale(30));
//        make.top.equalTo(self.buyBtn).offset(MBHeightScale(-12));
//    }];
  
//    [self.view addSubview:self.resumeBtn];
//    [self.resumeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.view.mas_top).offset(StatusBarAndNavigationBarHeight-22);
//        make.right.mas_equalTo(MBWidthScale(-20));
//    }];
    
    [self.view addSubview:self.privacyLab0];
    [self.privacyLab0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buyBtn.mas_bottom).offset(MBHeightScale(2));
//        make.left.mas_equalTo(MBWidthScale(20));
        make.right.equalTo(self.view.mas_centerX).offset(MBWidthScale(-30));
    }];
    
    [self.view addSubview:self.privacyLab1];
    [self.privacyLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.privacyLab0);
//        make.left.equalTo(self.privacyLab0.mas_right).offset(15);
        make.left.equalTo(self.view.mas_centerX).offset(MBWidthScale(-20));
    }];
    
//    [self.view addSubview:self.privacyLab2];
//    [self.privacyLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.privacyLab0);
//        make.left.equalTo(self.privacyLab1.mas_right).offset(15);
//    }];
    
    //确保在最外层
//    [self.view addSubview:self.vipPopView];
//    [self.vipPopView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
//    }];
}

#pragma mark - Setter && Getter

- (UIButton *)buyBtn {
    if (!_buyBtn) {
        _buyBtn = [UIButton new];
        [_buyBtn setBackgroundImage:ImageWithName(@"vip_purchase_back") forState:0];
        [_buyBtn setTitle:@"$29.99 Purchase Now" forState:0];
        _buyBtn.titleLabel.font = PDSemiBoldScale(14);
        [_buyBtn setTitleColor:HEXS(@"ffffff") forState:0];
        [_buyBtn addTarget:self action:@selector(PurchaseAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buyBtn;
}

- (UILabel *)privacyLab0 {
    if (!_privacyLab0) {
        _privacyLab0 = [UILabel new];
        _privacyLab0.userInteractionEnabled = YES;
        _privacyLab0.font = MBFontSizeScale(10);
        // 下划线
        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"PaidPolicy" attributes:attribtDic];
//        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"PrivacyPolicy" attributes:attribtDic];
        
        //赋值
        _privacyLab0.attributedText = attribtStr;
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick0)];
        
        [_privacyLab0 addGestureRecognizer:gestureRecognizer];
    }
    return _privacyLab0;
}

- (UILabel *)privacyLab1 {
    if (!_privacyLab1) {
        _privacyLab1 = [UILabel new];
        _privacyLab1.userInteractionEnabled = YES;
        _privacyLab1.font = MBFontSizeScale(10);
        // 下划线
        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"VIP Service Agreement" attributes:attribtDic];
        //赋值
        _privacyLab1.attributedText = attribtStr;
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick1)];
        
        [_privacyLab1 addGestureRecognizer:gestureRecognizer];

    }
    return _privacyLab1;
}

//- (UILabel *)privacyLab2 {
//    if (!_privacyLab2) {
//        _privacyLab2 = [UILabel new];
//        _privacyLab2.userInteractionEnabled = YES;
//        _privacyLab2.font = MBFontSizeScale(10);
//        // 下划线
//        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
//        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
////        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"About Subscription" attributes:attribtDic];
//        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"restore purchase" attributes:attribtDic];
//        //赋值
//        _privacyLab2.attributedText = attribtStr;
//        
//        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick2)];
//        
//        [_privacyLab2 addGestureRecognizer:gestureRecognizer];
//
//    }
//    return _privacyLab2;
//}
//
- (NSVipHeaderView *)vipHeaderView {
    if (!_vipHeaderView) {
        _vipHeaderView = [NSVipHeaderView new];
    }
    return _vipHeaderView;
}
//
//
//- (MBVipPopView *)vipPopView {
//    if (!_vipPopView) {
//        _vipPopView = [MBVipPopView new];
//        _vipPopView.hidden = YES;
//    }
//    return _vipPopView;
//}
//
//- (UIImageView *)dateImageView {
//    if (!_dateImageView) {
//        _dateImageView = [UIImageView new];
//        _dateImageView.image = ImageWithName(@"vipprofile_vip_30days_2");
//    }
//    return _dateImageView;
//}

- (NSVipFuncsView *)vipFuncsView {
    if (!_vipFuncsView) {
        __WeakSelf
        _vipFuncsView = [NSVipFuncsView new];
        _vipFuncsView.selectBlock = ^(NSString * _Nonnull productID, NSString * _Nonnull price) {
            weakSelf.productID = productID;
            [weakSelf.buyBtn setTitle:[NSString stringWithFormat:@"$%@ Purchase Now",price] forState:0];
        };
    }
    return _vipFuncsView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.bounces = NO;
        _scrollView.contentInset = UIEdgeInsetsMake(ScreenWidth*244/375-StatusBarAndNavigationBarHeight-MBHeightScale(24), 0, 0, 0);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = UIColor.clearColor;
    }
    return _scrollView;
}

- (NSVipBuyPopView *)vipBuyPopView {
    if (!_vipBuyPopView) {
        _vipBuyPopView = [[NSVipBuyPopView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    }
    return _vipBuyPopView;
}

- (UIImageView *)bottomImageView {
    if (!_bottomImageView) {
        _bottomImageView = [UIImageView new];
        _bottomImageView.image = ImageWithName(@"mine_buy_bottom_back");
    }
    return _bottomImageView;
}
@end
